package com.example.webdemo.service.impl;

import com.example.webdemo.dao.ArticleMapper;
import com.example.webdemo.model.Article;
import com.example.webdemo.model.Tag;
import com.example.webdemo.service.ArticlePopularityService;
import com.example.webdemo.service.ArticleService;
import com.example.webdemo.service.TagsService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zp
 * @version 1.0
 * @description: ArticleServiceImpl
 * @date 2023/7/31 19:13
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private TagsService tagsService;
    @Resource
    private ArticlePopularityService articlePopularityService;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return articleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Article record) {
        return articleMapper.insert(record);
    }

    @Override
    public int insertSelective(Article record) {
        return articleMapper.insertSelective(record);
    }

    @Override
    public Article selectByPrimaryKey(Integer id) {
        return articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Article record) {
        return articleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Article record) {
        return articleMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Article> selectAll() {
        return articleMapper.selectAll();
    }

    @Override
    public List<Article> selectByType(String type) {
        List<Article> articles = articleMapper.selectByType(type);
        if(articles==null){
            return new ArrayList<>();
        }
        List<Tag> tags = tagsService.selectAll();
        for (Article article: articles) {
            List<Tag> tagList = new ArrayList<>();
            String[] tagIds = article.getTags().split(",");
            for (String id: tagIds) {
                Integer tagId = Integer.parseInt(id);
                for (Tag tag: tags) {
                    if(tagId.equals(tag.getId())){
                        tagList.add(tag);
                    }
                }
            }
            article.setTagNames(tagList);
        }
        return articles;
    }

    @Override
    public List<Article> selectDraftArticle() {
        List<Article> articles = articleMapper.selectDraftArticle();
        if(articles==null){
            return new ArrayList<>();
        }
        List<Tag> tags = tagsService.selectAll();
        for (Article article: articles) {
            List<Tag> tagList = new ArrayList<>();
            String[] tagIds = article.getTags().split(",");
            for (String id: tagIds) {
                Integer tagId = Integer.parseInt(id);
                for (Tag tag: tags) {
                    if(tagId.equals(tag.getId())){
                        tagList.add(tag);
                    }
                }
            }
            article.setTagNames(tagList);
        }
        return articles;
    }

    @Override
    public List<Article> selectByUserId(Integer userId) {
        return articleMapper.selectByUserId(userId);
    }

    @Override
    public List<Article> selectCollectList(Integer userId) {
        List<Integer> ids = articlePopularityService.selectByUserIdAndTypeIds(userId, 2);
        if(CollectionUtils.isEmpty(ids)){
            return new ArrayList<>();
        }
        List<Article> articles = articleMapper.selectByIds(ids);
        if(articles==null){
            return new ArrayList<>();
        }
        List<Tag> tags = tagsService.selectAll();
        for (Article article: articles) {
            List<Tag> tagList = new ArrayList<>();
            String[] tagIds = article.getTags().split(",");
            for (String id: tagIds) {
                Integer tagId = Integer.parseInt(id);
                for (Tag tag: tags) {
                    if(tagId.equals(tag.getId())){
                        tagList.add(tag);
                    }
                }
            }
            article.setTagNames(tagList);
        }
        return articles;
    }
}
